package cn.uin.config;

import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Map;
import java.util.Properties;

/**
 * @ClassName MailConfig
 * @Description:邮件发送配置
 * @Author: Administrator
 * @CreateDate: 2025/10/27 15:53
 * @Version: 1.0
 */
@Configuration
@EnableConfigurationProperties(MailProperties.class)
public class MailConfig {

	@Bean
	public JavaMailSender mailSender(MailProperties mailProperties) {
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
		mailSender.setHost(mailProperties.getHost());
		mailSender.setPort(mailProperties.getPort());
		mailSender.setUsername(mailProperties.getUsername());
		mailSender.setPassword(mailProperties.getPassword());

		Map<String, String> properties = mailProperties.getProperties();
		Properties props = mailSender.getJavaMailProperties();
		props.put("mail.transport.protocol", mailProperties.getProtocol());
		props.put("mail.smtp.auth", properties.get("mail.smtp.auth"));
		props.put("mail.smtp.ssl.enable", "true");
		props.put("mail.smtp.starttls.enable", properties.get("mail.smtp.starttls.enable"));
		props.put("mail.debug", "true");
		return mailSender;
	}

}
